---
--类名: RewardFirstKillPanel.lua
--创建日期: 2019-01-24
--创建人: 宋建邦
--功能描述: view
--修改内容:

local IconRender = require("app.widget.renders.IconRender")
local HeadRender = require("app.widget.renders.HeadRender")

-- local RewardFirstKillPanel = class("RewardFirstKillPanel", app.mvc.ViewBase)
-- RewardFirstKillPanel.VIEW_ROOT_RES = Res.RewardFirstKillPanel
local RewardFirstKillPanel = app.mvc.createPanelClass(...)
function RewardFirstKillPanel.create(node)
	local sprite = RewardFirstKillPanel.extend(node or cc.CSLoader:createNode(Res.RewardFirstKillPanel))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end 

function RewardFirstKillPanel:init()
    self.preBtn = self:findChild("Panel/btn_1")
    self.nxtBtn = self:findChild("Panel/btn_2")

    self.preBtn:addClickEventListener(function()
        self:next(-1)
    end)

    self.nxtBtn:addClickEventListener(function()
        self:next(1)
    end)

    self.lblTitle = self:findChild("Panel/tlt")
    self.lblDesc = self:findChild("Panel/tips")

    self.lblT = self:findChild("Panel/tlt_1")
    self.lblA = self:findChild("Panel/tlt_2")

    self.lblT:setString(L"dungeon_firstKill_string3")
    self.lblA:setString(L"dungeon_firstKill_string4")

    self.imgBoss = self:findChild("Panel/Panel_mid/icon")
    self.imgBossKilled = self:findChild("Panel/Panel_mid/icon_kill")

    self.team5 = self:findChild("Panel/Panel_mid/Panel_5")
    self.team10 = self:findChild("Panel/Panel_mid/Panel_10")

    self.trGetRewardBtn = self:findChild("Panel/Panel_list_1/Button_lingqu")
    self.trFinishedBtn = self:findChild("Panel/Panel_list_1/Button_over")
    self.trGoToBtn = self:findChild("Panel/Panel_list_1/Button_go")

    self.arGetRewardBtn = self:findChild("Panel/Panel_list_2/Button_lingqu")
    self.arFinishedBtn = self:findChild("Panel/Panel_list_2/Button_over")
    self.arGoToBtn = self:findChild("Panel/Panel_list_2/Button_go")

    self.trGetRewardBtn:findChild("name"):setString(L("dungeon_firstKill_string7"))
    self.trFinishedBtn:findChild("name"):setString(L("dungeon_firstKill_string6"))
    self.trGoToBtn:findChild("name"):setString(L("dungeon_firstKill_string5"))

    self.arGetRewardBtn:findChild("name"):setString(L("dungeon_firstKill_string7"))
    self.arFinishedBtn:findChild("name"):setString(L("dungeon_firstKill_string6"))
    self.arGoToBtn:findChild("name"):setString(L("dungeon_firstKill_string5"))

    self.trGetRewardBtn:addClickEventListener(function()
        app:sendMsg("RewardModel", "reqGetFirstKillReward", self.dungeonId, 1, self.data.uuid)
    end)

    self.arGetRewardBtn:addClickEventListener(function()
        app:sendMsg("RewardModel", "reqGetFirstKillReward", self.dungeonId, 2, self.data.uuid)
    end)

    local function gotoKill()
        if self.curKill then
            local p = string.split(GD:getConvertResource(self.curKill.cfg.resource), "|")
            display.resourceGotoFeature({feature=p[1], param={p[3]}})
        end
    end

    self.trGoToBtn:addClickEventListener(function()
        gotoKill()
    end)

    self.arGoToBtn:addClickEventListener(function()
        gotoKill()
    end)

    self.tItems = {}
    self.aItems = {}
    self.t5 = {}
    self.t10 = {}
    -- self.curIdx = 1
    self.kills = {}

    for i = 1, 3 do 
        table.insert(self.tItems, self:findChild("Panel/Panel_list_1/icon_"..tostring(i)))
        table.insert(self.aItems, self:findChild("Panel/Panel_list_2/icon_"..tostring(i)))
    end

    for i = 1, 5 do
        table.insert(self.t5, self:findChild("Panel/Panel_mid/Panel_5/p"..tostring(i)))
    end

    for i = 1, 10 do
        table.insert(self.t10, self:findChild("Panel/Panel_mid/Panel_10/p"..tostring(i)))
    end
end

function RewardFirstKillPanel:setKillInfo(killInfo, idx)
    self.curKill = nil
    if not killInfo then 
        return
    end

    local info = killInfo.data
    local cfg = killInfo.cfg

    if not info or not cfg then
        return
    end
    self.curKill = killInfo

    self.dungeonId = info.dungeonSubID

    self.lblTitle:setString(L(cfg.name))
    self.lblDesc:setString(L("dungeon_firstKill_string1"))

    self.trGetRewardBtn:hide()
    self.trFinishedBtn:hide()
    self.trGoToBtn:hide()

    self.arGetRewardBtn:hide()
    self.arFinishedBtn:hide()
    self.arGoToBtn:hide()

    self.killed = true

    if info.isGetServerReward then
        self.arFinishedBtn:show()
    elseif info.canGetServerReward then
        self.arGetRewardBtn:show()
    else
        self.arGoToBtn:show()
        self.killed = false
    end

    if info.isGetMemberReward then
        self.trFinishedBtn:show()
    elseif info.canGetMemberReward then
        self.trGetRewardBtn:show()
    else
        self.trGoToBtn:setVisible(not self.killed)
    end

    self.imgBossKilled:setVisible(self.killed)
    self.imgBoss:loadTexture(Res.dungeonIconPath..tostring(cfg.dungeonBossIcon)..".png")
    self.imgBoss:setContentSize(self.imgBoss:getVirtualRenderer():getOriginalSize())

    self:setRewards(cfg)
    self:setMembers(info, cfg)
end

function RewardFirstKillPanel:setRewards(cfg)
    local drops1 = UD:parseDropIDToBagItem(cfg.reward1)
    local drops2 = UD:parseDropIDToBagItem(cfg.reward2)
    
    for i = 1, 3 do
        if drops1[i] then
            self.tItems[i]:show()
            local render = IconRender.create(self.tItems[i])
            render:setTipEnable(true)
            render:setTipUnShowOrigin(true)
            render:setData(drops1[i])
        else
            self.tItems[i]:hide()
        end

        if drops2[i] then
            self.aItems[i]:show()
            local render = IconRender.create(self.aItems[i])
            render:setTipEnable(true)
            render:setTipUnShowOrigin(true)
            render:setData(drops2[i])
        else
            self.aItems[i]:hide()
        end        
    end
end

function RewardFirstKillPanel:setMembers(info, cfg)
    local maxNum = checknumber(cfg.maxNum)
    local heads = nil
    if maxNum <= 5 then
        heads = self.t5
    else
        heads = self.t10
    end
    self.team5:setVisible(maxNum <= 5)
    self.team10:setVisible(not (maxNum <= 5))

    for i, v in ipairs(heads) do
        if self.killed then
            if info.passMembers and info.passMembers[i] then
                v:show()
                v:findChild("Panel"):setTouchEnabled(true)
                local h = HeadRender.create(v, 2)
                h:setData( cloneWithoutMeta(info.passMembers[i]) )
                h:onClick(function(sender)
                    if info.passMembers[i].uid ~= UD.player.uid then --点击别人的头像
                        local size = sender:getContentSize()
                        local pos = sender:convertToWorldSpace(cc.p(0, size.height/2))
                        UIHandler:showHeadMenu({online=true, attr=cloneWithoutMeta(info.passMembers[i]), pos=pos})
                    end
                end)
                if i == 1 then
                    v:findChild("Panel/icon_leader"):show()

                    local t = os.date("*t", info.passTime)
                    self.lblDesc:setString(
                        string.format(L("dungeon_firstKill_string2"), 
                        info.passMembers[i].name,
                        tostring(t.year), tostring(t.month), tostring(t.day)
                        )
                    )
                else
                    v:findChild("Panel/icon_leader"):hide()
                end
                v:findChild("Panel/icon"):show()
                v:findChild("Panel/icon_empty"):hide()
            else
                v:hide()
            end
        else
            v:show()
            v:findChild("Panel"):setTouchEnabled(false)
            v:findChild("Panel/Panel_lv"):hide()
            v:findChild("Panel/class_icon"):hide()
            v:findChild("Panel/icon_leader"):hide()

            v:findChild("Panel/icon"):hide()
            v:findChild("Panel/icon_empty"):show()
        end        
    end
end

function RewardFirstKillPanel:setData(data)
    self.data = data
    if self.curIdx == nil then
        self.curIdx = data.firstIndex
    end
    -- self.curIdx = 1
    self.kills = data.info
    self:next(0)
end

function RewardFirstKillPanel:next(off)
    if self.kills[checknumber(self.curIdx) + off] then
        self.curIdx = checknumber(self.curIdx) + off
        self:setKillInfo(self.kills[self.curIdx])
    end

    self.preBtn:setVisible(self.curIdx > 1)
    self.nxtBtn:setVisible(self.curIdx < #self.kills)
end

function RewardFirstKillPanel:onExit()
end

function RewardFirstKillPanel:refresh(data)
end

return RewardFirstKillPanel